We would like to use visualization to find out some patterns of felony frequency in NYC regarding time and date. We are interested in how month, day of the week, and time of the day are associated with felony frequency.
We would like to explore trends of felony crimes from 2016 to September 2022. We want to identify general trends over time and see if there are any significant changes before and during the COVID-19 pandemic.
num_days = function(month, year) {
year = as.integer(year)
months = 1:12
names(months) = month.abb
month = months[month]
as.numeric(strftime(as.Date(paste(year + month %/% 12, month %% 12 + 1, "01", sep = "-")) - 1, "%d"))
}
complaint %>%
filter(level == "FELONY") %>%
mutate(year = fct_rev(year)) %>%
group_by(year, month) %>%
dplyr::summarize(mean_freq = n() / num_days(month, year)) %>%
plot_ly(
x = ~month, y = ~year, z = ~mean_freq,
type = "heatmap"
) %>%
colorbar(x = 1, y = 1) %>%
layout(
title = "Average Daily Felony Frequency by Year and Month",
xaxis = list(title = "Month"),
yaxis = list(title = "Year")
)